<script src="js/md5.js"></script>
<script type="text/javascript">
	function AddLicensePageInit()
	{
		console.log("addLicensePageInit()");
		EnterKeyListenerForAddLicense();
	}
	
	function showAddLicenseModal(text){
		$(".addLicenseModal").fadeIn("slow");
		$("#name").focus();
	}
	
	function closeAddLicenseModal(){
		$(".addLicenseModal").fadeOut("slow");
	}
	
	//回车键监听函数
	function EnterKeyListenerForAddLicense(){
		console.log("start enter key listener");
		var event=arguments.callee.caller.arguments[0]||window.event;//消除浏览器差异  
	 	if (event.keyCode == 13){  
	 		addLicense();
	 	}  
	}
	
	function doSelectAllowAllActionEnable(obj,event){
		console.log("doSelectAllowAllActionEnable()");
		var allowAllActionEnable = $("#allowAllActionEnable").is(':checked')? 1: 0;
		console.log("doSelectAllowAllActionEnable allowAllActionEnable:" + allowAllActionEnable);
		if(allowAllActionEnable == 0)
		{
			$("#getAuthCodeEnable").prop("checked",false);
			$("#resetHistoryEnable").prop("checked",false);
			$("#recoverBackupEnable").prop("checked",false);
			$("#recycleBinEnable").prop("checked",false);
			//attr不能多次选择会无效
			//$("#getAuthCodeEnable").attr("checked",false);	
			//$("#resetHistoryEnable").attr("checked",false);	
			//$("#recoverBackupEnable").attr("checked",false);	
			//$("#recycleBinEnable").attr("checked",false);	
		}
		else
		{	
			$("#getAuthCodeEnable").prop("checked",true);
			$("#resetHistoryEnable").prop("checked",true);
			$("#recoverBackupEnable").prop("checked",true);
			$("#recycleBinEnable").prop("checked",true);
			//attr不能多次选择会无效
			//$("#getAuthCodeEnable").attr("checked","checked");
			//$("#resetHistoryEnable").attr("checked","checked");
			//$("#recoverBackupEnable").attr("checked","checked");
			//$("#recycleBinEnable").attr("checked","checked");
		}
	}
	
	function doSelectGetAuthCodeEnable(obj,event){
		console.log("doSelectGetAuthCodeEnable()");
		var getAuthCodeEnable = $("#getAuthCodeEnable").is(':checked')? 1: 0;
		console.log("doSelectGetAuthCodeEnable getAuthCodeEnable:" + getAuthCodeEnable);
		if(getAuthCodeEnable == 0)
		{
			$("#allowAllActionEnable").prop("checked",false);
		}
	}
	
	function doSelectResetHistoryEnable(obj,event){
		console.log("doSelectResetHistoryEnable()");
		var resetHistoryEnable = $("#resetHistoryEnable").is(':checked')? 1: 0;
		console.log("doSelectResetHistoryEnable resetHistoryEnable:" + resetHistoryEnable);
		if(resetHistoryEnable == 0)
		{
			$("#allowAllActionEnable").prop("checked",false);
		}
	}
	
	function doSelectRecoverBackupEnable(obj,event){
		console.log("doSelectRecoverBackupEnable()");
		var recoverBackupEnable = $("#recoverBackupEnable").is(':checked')? 1: 0;
		console.log("doSelectRecoverBackupEnable recoverBackupEnable:" + recoverBackupEnable);
		if(recoverBackupEnable == 0)
		{
			$("#allowAllActionEnable").prop("checked",false);
		}
	}
	
	function doSelectRecycleBinEnable(obj,event){
		console.log("doSelectRecycleBinEnable()");
		var recycleBinEnable = $("#recycleBinEnable").is(':checked')? 1: 0;
		console.log("doSelectRecycleBinEnable recycleBinEnable:" + recycleBinEnable);
		if(recycleBinEnable == 0)
		{
			$("#allowAllActionEnable").prop("checked",false);
		}
	}
	
	function getAllowedAction()
	{
		var allowAllActionEnable = $("#allowAllActionEnable").is(':checked')? 1: 0;
		var getAuthCodeEnable = $("#getAuthCodeEnable").is(':checked')? 1: 0;
		var resetHistoryEnable = $("#resetHistoryEnable").is(':checked')? 1: 0;
		var recoverBackupEnable = $("#recoverBackupEnable").is(':checked')? 1: 0;
		var recycleBinEnable = $("#recycleBinEnable").is(':checked')? 1: 0;

		var allowedAction = "{";		
		if(allowAllActionEnable == 1)
		{
			allowedAction += "allowAllAction:\"enabled\","
		}
		if(getAuthCodeEnable == 1)
		{
			allowedAction += "getAuthCode:\"enabled\","
		}
		if(resetHistoryEnable == 1)
		{
			allowedAction += "resetHistory:\"enabled\","
		}
		if(recoverBackupEnable == 1)
		{
			allowedAction += "recoverBackup:\"enabled\","
		}
		if(recycleBinEnable == 1)
		{
			allowedAction += "recycleBin:\"enabled\","
		}		
		allowedAction += "}";		

		console.log("getAllowedAction() allowedAction:" + allowedAction);
		return allowedAction;
	}
	
	function addLicense(obj,event){
		console.log("addLicense()");
		
		//Disable the obj
		$(obj).attr("disabled",true);
		
		var type = $("#lienceType").val();
		var serverSN = $("#serverSN").val();
		var customer = $("#customer").val();
	    var payInfo = $("#payInfo").val();
	    var usersCount =  $("#usersCount").val();
	    var validMonths =  $("#validMonths").val();
	    var allowedAction = getAllowedAction();
	    
	    console.log(customer,payInfo,usersCount,validMonths);
	    
        $.ajax({
            url : "/DocSystem/Sales/generateLicense.do",
            type : "post",
            dataType : "json",
            data : {
            	type: type,
            	serverSN : serverSN,
            	customer : customer,
            	payInfo : payInfo,
            	usersCount : usersCount,
            	validMonths : validMonths,
            	allowedAction: allowedAction,
            },
            success : function (ret) {
        		//Enable the obj
        		$(obj).attr("disabled",false);

                if( "ok" == ret.status ){
                	showLicenseList(gPageIndex);	//刷新LicenseList
            	    console.log("generateLicense Ok:",ret);        	   		
	            	window.location.href = ret.data;
	            	showErrorMessage("新增成功");
                }else {
                	showErrorMessage("错误：" + ret.msgInfo);
                }
            },
            error : function () {
        		//Enable the obj
        		$(obj).attr("disabled",false);

        		showErrorMessage("新增失败:服务器异常");
            }
        });
	}
	
	//页面初始化代码    
    $(function(){
    	console.log("addLicense Page init");
    	AddLicensePageInit();
		$("#customer").click().focus();
	});
</script>
                    
                  <div class="form-horizontal">
                    <!-- type -->
                    <div class="form-group">
                      <label class="control-label col-lg-3">证书类型</label>
                      <div class="col-lg-9">
                        <select class="form-control" id="lienceType" name="lienceType">
                          <option value="2">专业版</option>
                          <option value="1">企业版</option>
                        </select>
                      </div>
                    </div>
                    <!-- serverSN -->
                    <div class="form-group">
                      <label class="control-label col-lg-3" for="serverSN">设备号</label>
                      <div class="col-lg-9">
                        <textarea class="form-control" id="serverSN" name="serverSN"></textarea>
                      </div>
                    </div>
                    <!-- customer -->
                    <div class="form-group">
                      <label class="control-label col-lg-3" for="customer">客户信息</label>
                      <div class="col-lg-9">
                        <input type="text" class="form-control" id="customer" name="customer">
                      </div>
                    </div>
                    <!-- payInfo -->
                    <div class="form-group">
                      <label class="control-label col-lg-3" for="payInfo">支付信息</label>
                      <div class="col-lg-9">
                        <input type="text" class="form-control" id="payInfo" name="payInfo">
                      </div>
                    </div>
                    <div class="form-group">
                      <label class="control-label col-lg-3">用户数量</label>
                      <div class="col-lg-9">
                        <select class="form-control" id="usersCount" name="usersCount">
                          <option value="20">20人</option>
                          <option value="30">30人</option>
                          <option value="40">40人</option>
                          <option value="50">50人</option>
                          <option value="60">60人</option>
                          <option value="70">70人</option>
                          <option value="80">80人</option>
                          <option value="90">90人</option>
                          <option value="100">100人</option>
                          <option value="200">200人</option>
                          <option value="300">300人</option>
                          <option value="400">400人</option>
                          <option value="500">500人</option>
                          <option value="600">600人</option>
                          <option value="700">700人</option>
                          <option value="800">800人</option>
                          <option value="900">900人</option>
                          <option value="1000">1000人</option>
                          <option value="2000">2000人</option>
                          <option value="3000">3000人</option>
                          <option value="4000">4000人</option>
                          <option value="5000">5000人</option>
                          <option value="100000">不限</option>
                        </select>
                      </div>
                    </div>
                    <div class="form-group">
                      <label class="control-label col-lg-3">有效时间</label>
                      <div class="col-lg-9">
                        <select class="form-control" id="validMonths" name="validMonths">
                          <option value="1">1个月</option>
                          <option value="2">2个月</option>
                          <option value="3">3个月</option>
                          <option value="4">4个月</option>
                          <option value="5">5个月</option>
                          <option value="6">6个月</option>
                          <option value="7">7个月</option>
                          <option value="8">8个月</option>
                          <option value="9">9个月</option>
                          <option value="10">10个月</option>
                          <option value="11">11个月</option>
                          <option value="12">1年</option>
                          <option value="24">2年</option>
                          <option value="36">3年</option>
                          <option value="240" selected="selected">长期</option>
                        </select>
                      </div>
                    </div>
                    <div class="form-group">
                      <label class="control-label col-lg-3">高级选项</label>
                      <div class="col-lg-9" style="margin-top:10px">
	                      <span>
							<input type="checkbox" id="getAuthCodeEnable" value="0" onclick="doSelectGetAuthCodeEnable(this,event)">第三方接入
						  </span> 	
	                      <span style="margin-left:20px">
							<input type="checkbox" id="resetHistoryEnable" value="0" onclick="doSelectResetHistoryEnable(this,event)">历史回退
						  </span> 	
	                      <span style="margin-left:20px">
							<input type="checkbox" id="recoverBackupEnable" value="0" onclick="doSelectRecoverBackupEnable(this,event)">备份恢复
						  </span> 							  
	                      <span style="margin-left:20px">
							<input type="checkbox" id="recycleBinEnable" value="0" onclick="doSelectRecycleBinEnable(this,event)">回收站
						  </span> 							  
	                      <span style="margin-left:20px">
							<input type="checkbox" id="allowAllActionEnable" value="0" onclick="doSelectAllowAllActionEnable(this,event)">全选
						  </span> 	
                      </div>
                    </div>
                    <!-- Buttons -->
                    <div class="form-group">
                      <!-- Buttons -->
                      <div class="col-lg-offset-9 col-lg-2">
                        <button type="submit" class="btn btn-success" id="btn_addLicense" onclick="addLicense(this,event)">添加</button>
                      </div>
                    </div>
                  </div>
